package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.SystemdictionaryItem;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.SystemdictionaryItemQuery;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.service.ISystemdictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemdictionaryItem")
public class SystemdictionaryItemController {

    @Autowired
    private ISystemdictionaryItemService systemdictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequiresPermissions(value={"字典明细列表","systemdictionaryItem:list"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") SystemdictionaryItemQuery qo, Model model) {
        if(qo.getParentId()!=null)
        {
            model.addAttribute("pageInfo", systemdictionaryItemService.query(qo));
            model.addAttribute("dic", systemDictionaryService.get(qo.getParentId()));
        }
        model.addAttribute("result", systemDictionaryService.list());
        return "/systemdictionaryItem/list";
    }
    @RequiresPermissions(value={"字典明细编辑","systemdictionaryItem:input"},logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("entity", systemdictionaryItemService.get(id));
        }
        return "systemdictionaryItem/input";
    }

    @RequiresPermissions(value={"字典明细保存或更新","systemdictionaryItem:saveOrUpdate"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(SystemdictionaryItem entity) {

        JsonResult result = new JsonResult();
        try {
            //int a=1/0;
            systemdictionaryItemService.saveOrUpdate(entity);
        }catch (Exception e)
        {
//            e.printStackTrace();
            result.mark("保存失败");
        }
        return result;
    }
    @RequiresPermissions(value={"字典明细删除","systemdictionaryItem:delete"},logical = Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {

            systemdictionaryItemService.delete(id);
        }catch (Exception e)
        {
            result.mark("亲,执行失败!");
        }
        return result;
    }
}
